import { Component, OnInit } from '@angular/core';
import { CapacityChartComponent } from './capacity-chart/capacity-chart.component';
import { Capacity } from '../../../entity/capacity.model';
import { CapacityViewService } from '../../../service/capacity-view.service';
import { ApplicationService } from "../../../service/application.service";
import { Observable } from "rxjs/Observable";
import { DatePipe } from "@angular/common";
import { Farm } from "../../../entity/farm.model";
import { FarmService } from "../../../service/farm.service";
import { count } from 'rxjs/operators';

@Component({
  selector: 'app-capacity-view',
  templateUrl: './capacity-view.component.html',
  styleUrls: ['./capacity-view.component.css']
})
export class CapacityViewComponent implements OnInit {
  private fomat: string = "yyyy-MM-dd";
  /**
 * 时间当前选中下标
 * @type {number}@memberof CapacityViewComponent
 */
  public active: number = 0;
  /**
  * 公司全年每月发电量
  * @type {number}@memberof CapacityViewComponent
  */
  public compCapacity: Capacity[] = [];
  /**
* 当月各风场发电量
* @type {number}@memberof CapacityViewComponent
*/
  public farmCapacity: Capacity[] = [];
  /**
* 各风场全年发电量
* @type {number}@memberof CapacityViewComponent
*/
  public farmYearCapacity: Capacity[] = [];
  /**
* 当前风场每月发电量
* @type {number}@memberof CapacityViewComponent
*/
  public farmMonthCapacity: Capacity[] = [];

  public xAxisMonth: string[] = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
  public xAxisFarm: string[];
  private farms: Farm[] = [];
  private farmId: number;
  /**
  * 样式判断条件
  * @type {number}@memberof CapacityViewComponent
  */
  private date = new Date();
  public currtM: string = (this.date.getMonth() + 1).toString() + '月';
  public currtF: string;
  constructor(
    private capacityViewService: CapacityViewService,
    private datePipe: DatePipe,
    private farmService: FarmService,
    private application: ApplicationService,
  ) { }

  ngOnInit() {
    this.farmService.getFarmsWithTotalTurbineByUserId(this.application.currentUser.userId)
      .then(res => {
        this.farms = [...res];
        this.currtF = this.farms[0].farmName;
      })
      .catch(error => {
        console.log('app-capacity-view', error);
      });
    this.initData();
  }

  onClickHeader(a: number) {
    this.active = a;
    this.farmMonthCapacity = [];
    this.farmYearCapacity = [];
    this.compCapacity = [];
    this.farmCapacity = [];
    if (this.active) {  // 场站电量；
      const date = null;
      this.farmId = this.farms[0].farmId;
      let getPowerYest = this.capacityViewService.getYearPowers(this.farmId);
      let getPowerMonth = this.capacityViewService.getMonthPowers(date);
      Promise.all([getPowerYest, getPowerMonth])
        .then(res => {
          if (res) {
            this.farmMonthCapacity = [...res[0]];
            this.farmMonthCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            // const arrC = this.getMonthStr(this.farmMonthCapacity);
            // this.xAxisMonth = [...arrC];
            this.farmYearCapacity = [...res[1]];
            this.currtF = this.farmYearCapacity[0].farmName;
            this.farmYearCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            // const arrF = this.getFarmStr(this.farmYearCapacity);
            this.xAxisFarm = [...this.farmYearCapacity].map(f => f.farmName);
          }
        })
        .catch(error => {
          console.log('app-capacity-view', error);
        })
    } else {  // 月度电量
      const date = new Date(this.date.getFullYear(), this.date.getMonth());
      let dateFomat: string = "";
      dateFomat = this.datePipe.transform(date, this.fomat);
      this.farmId = 0;
      let getPowerYest = this.capacityViewService.getYearPowers(this.farmId);
      let getPowerMonth = this.capacityViewService.getMonthPowers(dateFomat);
      Promise.all([getPowerYest, getPowerMonth])
        .then(res => {
          if (res) {
            this.compCapacity = [...res[0]];
            this.compCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            // const arrC = this.getMonthStr(this.compCapacity);
            // this.xAxisMonth = [...arrC];
            this.farmCapacity = [...res[1]];
            this.farmCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            // const arrF = this.getFarmStr(this.farmCapacity);
            // this.xAxisFarm = [...arrF];
            this.xAxisFarm = [...this.farmCapacity].map(f => f.farmName);
          }
        })
        .catch(error => {
          console.log('app-capacity-view', error);
        })

    }
  }
  initData(): any | Observable<any> | Promise<any> {
    this.compCapacity = [];
    this.farmCapacity = [];
    const date = new Date(this.date.getFullYear(), this.date.getMonth());
    let dateFomat: string = "";
    dateFomat = this.datePipe.transform(date, this.fomat);
    this.farmId = 0;
    let getPowerYest = this.capacityViewService.getYearPowers(this.farmId);
    let getPowerMonth = this.capacityViewService.getMonthPowers(dateFomat);
    Promise.all([getPowerYest, getPowerMonth])
      .then(res => {
        if (res) {
          this.compCapacity = [...res[0]];
          this.compCapacity.forEach(o => {
            o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
          });
          // const arrC = this.getMonthStr(this.compCapacity);
          // this.xAxisMonth = [...arrC];
          this.farmCapacity = [...res[1]];
          this.farmCapacity.forEach(o => {
            o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
          });
          // const arrF = this.getFarmStr(this.farmCapacity);
          // this.xAxisFarm = [...arrF];
          this.xAxisFarm = [...this.farmCapacity].map(f => f.farmName);
        }
      })
      .catch(error => {
        console.log('app-capacity-view', error);
      })
  }

  getCurrentSign(currt: string) {
    if (this.active) {
      // 点击场站名称
      this.farmMonthCapacity = [];
      this.currtF = currt;
      let f = this.farms.find(f => f.farmName === this.currtF);
      this.capacityViewService.getYearPowers(f.farmId)
        .then(res => {
          if (res) {
            this.farmMonthCapacity = [...res];
            this.farmMonthCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            // const arrC = this.getMonthStr(this.farmMonthCapacity);
            // this.xAxisMonth = [...arrC];
          }
        })
        .catch(error => {
          console.log('app-capacity-view', error);
        })
    } else {
      // 点击公司级月份
      this.farmCapacity = [];
      this.currtM = currt;
      let m = this.currtM.substring(0, this.currtM.length - 1)
      const dt = new Date(this.date.getFullYear(), Number(m) - 1);
      let dateFomat: string = "";
      dateFomat = this.datePipe.transform(dt, this.fomat);
      this.capacityViewService.getMonthPowers(dateFomat)
        .then(res => {
          if (res) {
            this.farmCapacity = [...res];
            this.farmCapacity.forEach(o => {
              o.completiones = o.plantPower ? (o.adjustPower / o.plantPower) * 100 : 0;
            });
            this.xAxisFarm = [...this.farmCapacity].map(f => f.farmName);
            // const arrF = this.getFarmStr(this.farmCapacity);
            // this.xAxisFarm = [...arrF];
          }
        })
        .catch(error => {
          console.log('app-capacity-view', error);
        })
    }
  }

  /* getMonthStr(compCapacity: Capacity[]) {
    const arrC: any[] = [];
    compCapacity.forEach((c, index) => {
      let arr = [`${index + 1}月`, `\n<span style='color:#0ff'>${c.adjustPower}</span>`, c.plantPower.toFixed(2), c.lossPower.toFixed(2)];
      let s = arr.join(' ');
      arrC.push(s);
    })

    return arrC;
  }

  getFarmStr(farmCapacity: Capacity[]) {
    const arrF: any[] = [];
    farmCapacity.forEach(p => {
      let arr = [p.farmName, p.adjustPower.toFixed(2), p.plantPower.toFixed(2), p.lossPower.toFixed(2)]
      let s = arr.join(' ');
      arrF.push(s);
    })

    return arrF;
  } */

}



// WEBPACK FOOTER //
// ./src/app/module/monitor/capacity-view/capacity-view.component.ts